# -*- coding: utf-8 -*-
"""
Created on Fri Nov 09 22:35:45 2012

@author: Ago
"""
import vtk


class Zob3D():
  
        
    def __init__(self, instSp):
        
        self.instSp=instSp
        
    def modelNow(self, model):
        self.modelStl=str(model)
        self.model.SetFileName(self.modelStl)
        self.model.Update()
       

    def zobraz3D(self, modelStl):        
        
        self.modelStl=str(modelStl)
        self.model = vtk.vtkSTLReader()
        #self.instSp.listWidget.addItem(self.modelStl)
        
        self.model.SetFileName(self.modelStl)
        #model.Update()

        surfaceFilter = vtk.vtkDataSetSurfaceFilter()
        surfaceFilter.SetInput(self.model.GetOutput())
        surfaceFilter.Update()         
        cowNormals = vtk.vtkPolyDataNormals()
        cowNormals.SetInputConnection(surfaceFilter.GetOutputPort())
        
        self.ren = vtk.vtkRenderer()
        self.renWin = vtk.vtkRenderWindow()
        self.renWin.AddRenderer(self.ren)
        
        transform = vtk.vtkTransform()
        transform.Scale(1, 1, 1)
        transF = vtk.vtkTransformPolyDataFilter()
        transF.SetInputConnection(self.model.GetOutputPort())
        transF.SetTransform(transform)
        
        self.plane = vtk.vtkPlane()
        self.plane.SetOrigin(0, 0, 0) # výška řezu
        self.plane.SetNormal(0, 0.0000001, -0.1)  # keep everything in direction of normal
        xplaneColl = vtk.vtkPlaneCollection()
        xplaneColl.AddItem(self.plane)  

   
        self.plane1 = vtk.vtkPlane()
        self.plane1.SetOrigin(0, 0, 0) # výška řezu -zobrazení v okně
        self.plane1.SetNormal(0, 0.0000001, 0.1)  # náklon - bez náklonu chyby
        xplaneColl1 = vtk.vtkPlaneCollection()
        xplaneColl1.AddItem(self.plane1)
        
        self.clip = vtk.vtkClipClosedSurface()
        self.clip.SetClippingPlanes(xplaneColl)
        self.clip.SetInputConnection(transF.GetOutputPort()) #(glyphs.GetOutputPort())
        #self.clip.SetActivePlaneColor(1.0,0,0)
        self.clip.GenerateFacesOff()
        self.clip.GenerateFacesOn()
        
        self.normalsM = vtk.vtkPolyDataNormals()
        self.normalsM.SetInputConnection(self.clip.GetOutputPort())
        self.normalsM.FlipNormalsOn()        
        
        self.clip1 = vtk.vtkClipClosedSurface()
        self.clip1.SetClippingPlanes(xplaneColl1)
        self.clip1.SetInputConnection(self.clip.GetOutputPort())
        #self.clip1.SetActivePlaneColor(1.0,0,0)
        self.clip1.GenerateFacesOff()
        self.clip1.GenerateFacesOn()        
        
        
        self.normals = vtk.vtkPolyDataNormals()
        self.normals.SetInputConnection(self.clip1.GetOutputPort())
        self.normals.FlipNormalsOn()
        #normals.FlipNormalsOff()
        
        self.cutMapper = vtk.vtkDataSetMapper()
        self.cutMapper.SetInputConnection(self.normals.GetOutputPort())
        self.cutMapper.ScalarVisibilityOff()
        
        self.mapperM = vtk.vtkDataSetMapper()
        self.mapperM.SetInputConnection(self.normalsM.GetOutputPort())
        self.mapperM.ScalarVisibilityOff()
        
        actor = vtk.vtkActor()
        actor.SetMapper(self.cutMapper)
        actor.GetProperty().SetColor(0,1,0)   # barva modelu
        
        actorM = vtk.vtkActor()
        actorM.SetMapper(self.mapperM)
        actorM.GetProperty().SetColor(0.0, 1.0, 0.0)
        actorM.GetProperty().SetOpacity(0.1)

        iren = vtk.vtkRenderWindowInteractor()
        interactorStyle = vtk.vtkInteractorStyleTrackballCamera()
        iren.SetInteractorStyle(interactorStyle)
        iren.SetRenderWindow(self.renWin)        
        #Rámeček  - Create plane stuff
        planeWidget = vtk.vtkImplicitPlaneWidget()
        planeWidget.SetNormalToZAxis(1)
        #planeWidget.
        planeWidget.DrawPlaneOff()
        planeWidget.SetInteractor(iren)
        planeWidget.SetPlaceFactor(1.8) #velikost rámečku
        planeWidget.SetInput(cowNormals.GetOutput())
        planeWidget.PlaceWidget()
        planeWidget.AddObserver("InteractionEvent", self.myCallback)
        planeWidget.On()        
        
        self.ren.AddActor(actor)   # řez 2x (ze zhora - ze spodu)
        self.ren.AddActor(actorM)  # celý model
        #cam.Elevation(20.0)
        self.ren.SetBackground(0.0, 0.0, 0.0) # Barva pozadí
        self.ren.GetActiveCamera().Azimuth(85)
        #ren.GetActiveCamera().Elevation(3)
        self.ren.GetActiveCamera().Dolly(0.0050) # zvětšení
        self.ren.GetActiveCamera().Zoom(1.3)
        self.ren.GetActiveCamera().Roll(270)
        #ren.GetActiveCamera().SetDistance(40)
        self.ren.GetActiveCamera().Yaw(2)
        self.ren.ResetCameraClippingRange()        
#        ren.GetActiveCamera().ParallelProjectionOn() 
        #cam.Azimuth(55)   # Elevation would spin about X
        self.renWin.SetSize(500,400)        
        iren.Initialize()        
        self.renWin.Render()
        iren.Start()
	        
    
    def cut(self, v):

        self.mapperM.Update()
        self.cutMapper.Update()
        self.renWin.Render()        


        #Zpětné volání - callback function 
    def myCallback(self, obj, event):
        
        global plane, selectActor
        obj.GetPlane(self.plane)
        #obj.GetPlane(self.plane1)
        self.cut(0.0)# Přiřaďte řádku widget s interactor - Associate the line widget with the interactor	


        
#zobraz = Zob3D("test")
#zobraz.zobraz3D("Model_Stl/kuzel.stl")
